package presence.demo;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import passengers.demo.MultipleSmsMainActivity;
import presence.demo.R;

import database.demo.DataBaseWrapperPassengers;
import database.demo.PassengersOperations;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;


/**
 * saves the last presence check
 */
public class PresenceMainActivity extends Activity {
	private PassengersOperations passengerDBoperation;
	TextView tmpId;
	CheckBox tmpPresence;
	TextView tmpUpdate;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.presence_view);
		//closes keyboard 
		this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		//
		passengerDBoperation = new PassengersOperations(this);
		passengerDBoperation.open();
		tmpUpdate = (TextView)findViewById(R.id.extraLable);
		//-------------
		Cursor update = passengerDBoperation.getUpdate();
		//Toast toast = Toast.makeText(getApplicationContext(),update.toString(), Toast.LENGTH_LONG);
		//toast.show();
		//if(!(update!=null )){
		//	tmpUpdate.setText(update.getString(7));
		//}
		populatePassengerList();
	}
	
	//get all passengers from DB
	private void populatePassengerList() {
		boolean exists=false;
		Cursor cursor = passengerDBoperation.getAllPassengers();
		
		// Allow activity to manage lifetime of the cursor.
		// DEPRECATED! Runs on the UI thread, OK for small/short queries.
		startManagingCursor(cursor);
		
		// Setup mapping from cursor to view fields:
		String[] fromFieldNames = new String[] 
				{DataBaseWrapperPassengers.PASSENGER_ID,DataBaseWrapperPassengers.PASSENGER_LASTNAME, 
				DataBaseWrapperPassengers.PASSENGER_FIRSTNAME,DataBaseWrapperPassengers.PASSENGER_PRESENT};
		int[] toViewIDs = new int[]
				{R.id.idTextLable,R.id.dname_text,R.id.firstText, R.id.ppresent};
		
		// Create adapter to may columns of the DB onto elements in the UI.
		SimpleCursorAdapter myCursorAdapter = 
				new SimpleCursorAdapter(
						this,		// Context
						R.layout.presence_item_view,	// Row layout template
						cursor,					// cursor (set of DB records to map)
						fromFieldNames,			// DB Column names
						toViewIDs				// View IDs to put information in
						);
		
		myCursorAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
		    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
		    	if(columnIndex == 6) {
		    		CheckBox cb = (CheckBox) view;
		    		cb.setChecked(cursor.getInt(6) > 0);
		    		return true;
		    	}
		    	return false;
		    }
		});
		
		// Set the adapter for the list view
		final ListView myList = (ListView) findViewById(R.id.passListView);
		myList.setAdapter(myCursorAdapter);	
		tmpUpdate = (TextView)findViewById(R.id.extraLable);
		int count = myList.getAdapter().getCount();
		if (count >0){
			exists=true;
			Cursor update = passengerDBoperation.getUpdate();
			tmpUpdate.setText(update.getString(7));
		}
		else{
			Toast.makeText(PresenceMainActivity.this,
					"No available passengers", Toast.LENGTH_SHORT).show();
		}
		
		if (exists){
			// Clear All
			Button btnClearAll = (Button) findViewById(R.id.btn_clear);
			btnClearAll.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
			int count = myList.getAdapter().getCount();
			for (int i = 0; i < count; i++) {
			RelativeLayout itemLayout = (RelativeLayout)myList.getChildAt(i); // Find by under LinearLayout
			CheckBox checkbox = (CheckBox)itemLayout.findViewById(R.id.ppresent);
			checkbox.setChecked(false);
			
			} 
			}
			});
			
			// Save
			Button btnsave = (Button) findViewById(R.id.save_btn);
			btnsave.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
			int count = myList.getAdapter().getCount();
			for (int i = 0; i < count; i++) {
				RelativeLayout itemLayout = (RelativeLayout)myList.getChildAt(i); // Find by under LinearLayout
				tmpPresence = (CheckBox)itemLayout.findViewById(R.id.ppresent);
				tmpId = (TextView) itemLayout.findViewById(R.id.idTextLable);
				Calendar cal = Calendar.getInstance();
				SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM d, yyyy    HH:mm");
				String tmpTime = sdf.format(cal.getTime());
							
				passengerDBoperation.updatePassengerPresene(tmpId.getText().toString(), null, tmpPresence.isChecked(), tmpTime);
				} 
			tmpUpdate = (TextView)findViewById(R.id.extraLable);
			Cursor update = passengerDBoperation.getUpdate();
			tmpUpdate.setText(update.getString(7));
			Toast.makeText(PresenceMainActivity.this,
					"Updated!", Toast.LENGTH_LONG).show();	
			}
			});
		}
	}
		
	
	@Override
	protected void onDestroy() {
		super.onDestroy();	
		passengerDBoperation.close();
	}


}









